#!/usr/bin/python
# Talk Like a Pirate day Script by: Inc and h3k
# License: BSD license (http://www.opensource.org/licenses/bsd-license.php)
# Contact: inc@inc-corporate.org 
# version 1.0
# Hackabot port by Michael Marineau

##HACKABOT_HELP##
# Talk like a pirate!
# !pirate [nick] | a phrase | --pickupchick | --pickupguy | --saying
##HACKABOT_HELP##

import random
from random import randint
import sys
import os
import re
from amara import binderytools
import MySQLdb

config = os.getenv("HACKABOT_CFG")
if (config == None):
	sys.stderr.write("imitate: HACKABOT_CFG is undefined!\n")
	sys.exit(1)

fullcfg = binderytools.bind_file(config).hackabot
database = fullcfg.cmdconfig.database


pirate_sayings = [
    "Bring me one noggin of rum, now, won't you, matey.",
    "Avast, there!",
    "Dead men don't bite",
    "Shiver my timbers!",
    "Fifteen men on the dead man's chest- Yo-ho-ho, and a bottle of rum!",
    "Avast belay, yo ho, heave to, A-pirating we go, And if we're parted by a shot, We're sure to meet below!",
    "Yo ho, yo ho, the pirate life,The flag o'skull and bones. A merry hour, a hempen rope, And hey for Davy Jones.",
    "C'mere me beauty",
    "Is that a hornpipe in your pocket, or are you just glad to see me? or both?",
    "smartly, me lass",
    "well, me hearties, let's see what crawled out of the bung hole.",
    "You little land lubber!"
]
# heres the pirates guide to picking up chicks
pirate_pickups = [
    "Avast, me proud beauty! Wanna know why my Roger is so Jolly?",
    "Have ya ever met a man with a real yardarm",
    "Come on up and see me urchins",
    "Yes, That is a hornpipe in pocket and I'm happy to see you",
    "I'd love to drop achor in your lagoon.",
    "Pardon me, but would ya mind if fired me cannon through your porthole?",
    "How'd you like to scrape the barnacles off of me rudder?",
    "Ya know, darlin', I'm 97 percent chum free.",
    "Well blow me down?",
    "Prepare to be boarded",
    "They don't call me Long John because my head is so big",
    "You're drinking a Salty Dog? How'd you like to try the real thing?",
    "Wanna shiver me timbers?",
    "I've sailed the seven seas, and you're the sleekest schooner I've ever sighted!",
    "Brwaack! Polly want a cracker? ... Oh, wait. That's for Talk like a Parrot Day.",
    "That's the finest pirate booty I've ever laid eyes on.",
    "Let's get together and haul some keel",
    "that's some treasure chest you've got there."
]
# heres the lady pirates guide to picking up guys
chick_pickups = [
    "What are YOU doing here?",
    "Is that a belayin' pin in yer britches, or are ye...(you can finish this one)",
    "Come show me how ye bury yer treasure!",
    "Thats quiet a cutlass ye got there what ye need is a good scabbard!",
    "So tell me, why do they call ye, captain featherswort?",
    "I've crushed seventeen men's skills between me thighs",
    "C'mon lad, shiver me timbers!",
    "RAMMING SPEED!"
    "you. Pants off. NOW!"
]
pirate_dictionary = {
    'lover':'lubber',
    'hi':'ahoy',
    'friend':'bucko',
    'hello':'ahoy!',
    'attention':'Alas!',
    'beautiful': 'Avast',
    'sexy':'Avast!',
    'agree':'Aye!',
    'agreement':'Aye!',
    'yes':'Aye,Aye sir!',
    'sure':'Aye, Aye!',
    'yes sir':'Aye, Aye Sir!',
    'good':'ARR!',
    'splended':'arr!',
    'great':'ARR!',
    'anger':'blimey!',
    'insult':'NEVER TURN YOUR BACK SCURVY DOG!',
    'enemy':'scurvy!',
    'stop':'BELAY!',
    'die':'Dead men tell no tales',
    'clear passage': 'Gangway',
    'my':'me',
    'dead':'Hes gone to Davy Jones locker',
    'annoyed':'Hang the jib!',
    'annoy':'Hang the jib!',
    'surprise':'sink me!',
    'comrade':'Me hearties',
    'buddy':'Me hearties',
    'sudden':'Shiver me timbers!',
    'quickly':'Smartly',
    'drink':'Spliced the Mainbrace!',
    'drunk':'spliced the Mainbrace!',
    'you': 'Ye',
    'forward':'fore',
    'after':'aft',
    'nonsense':'Bilge!',
    'ship-hull':'bilge',
    'bad-water':'Bilge water',
    'vodka':'Clap of thunder!',
    'three-masted vessel':'Mizzenmast',
    'three':'Mizzen',
    'upper-deck':'Poop deck',   
    'nose':'Prow',
    'left-side of the ship':'port',
    'eyes':'dealights',
    'arrangement':'rigging',
    'right-side ship':'starboard',
    'pirate':'Buccaneer',
    'robber':'Buccaneer',
    'crew':'Hands',
    'group':'Hands',
    'executioner':'Jack Ketch',
    'sailor':'Jack Tar',
    'young-man':'lad',
    'boy':'lad',
    'girl':'lass',
    'young-woman':'lass',
    'Sociable':'matey',
    'friendly':'matey',
    'another-pirate':'matey',
    'lawyer':'messdeck',
    'know-it-all':'messdeck',
    'pirate-life':'on the account',
    'pirate life':'on the account',
    'life':'on the account',
    'private-ship':'Privateer',
    'scoundrel':'Scallywag',
    'deceitful':'scallywag',
    'vile':'Scurvy',
    'low':'scurvy',
    'vulgar':'scurvy',
    'contemptible':'scurvy',
    'raw':'sprogs',
    'children':'sprogs',     
    'buffoon':'squiffy',
    'supplier':'sutler',
    'stolen-goods':'booty',
    'piracy':'booty',
    'goods':'booty',  
    'punishment':'Cat O-Nine Tails',
    'sailer-song':'Chantey, Shantey, Shanty',
    'sword':'Cutlass',        
    'coin':'Doubloons',
    'gold-coin':'Doubloons',
    'liquor':'grog',
    'noose':'Hempen Halter',
    'black-flag':'Jolly Roger',
    'silver-coin':'Pieces of Eight',
    'license-to-seize':'Letter of Marque',
    'whip': 'ropes end',
    'rope':'ropes end',
    'cannons':'Six Pounders',
    'prison':'The hulks',
    'death-mark':'black spotted',
    'lean':'Careen',
    'dual-shots':'Chain shot',
    'pursuit':'chase',
    'cheat':'hornswaggle',
    'haul':'keelhaul',
    'abandon':'Maroon',
    'overtake':'Overhaul',
    'gain':'Overhaul',
    'cut-ship':'Scuttle',
    'clean':'titivate',
    'force-walk':'Walk the plank',
    'cast-three-sails':'To be Three Sheets in the Wind',
    'lift-anchor':'Weigh anchor'
}

def gettext(chan, nick = None):
	try:
		db = MySQLdb.connect(host=str(database.host), \
			db=str(database.name), \
			user=str(database.user), passwd=str(database.passwd))
	except MySQLdb.Error, e:
		sys.stderr.write("imitate: MySQL error %d: %s" % (e.args[0], e.args[1]))
		sys.exit(1)

	chan = db.escape_string(chan)
	cursor = db.cursor()
	if nick:
		nick = db.escape_string(nick)
		nickq = " AND nick = '"+nick+"' "
	else:
		nickq = " "
	cursor.execute("SELECT id, nick, chan, text, type FROM log WHERE chan = '"+chan+"' AND ( type = 'msg' OR type = 'action' ) "+nickq+" AND text NOT LIKE '!%' ORDER BY id DESC LIMIT 1")
	rows = cursor.fetchall()
	cursor.close()
	db.close()

	return rows[0][3]

def translate(text):
	text = text.split(' ')
	output = ""
	for word in text:
		if pirate_dictionary.has_key(word):
			output += pirate_dictionary[word]+' '
		else:
			output += word+' '
	return output
    
chan = None
msg = None
for line in sys.stdin.readlines():
	if re.match(r'to\s+(#\S+)', line):
		c = re.match(r'to\s+(#\S+)', line)
		chan = c.group(1)
	elif re.match(r'msg\s+(\S+.*)', line):
		c = re.match(r'msg\s+(\S+.*)', line)
		msg = c.group(1)

if msg and re.match(r'--pickupchick', msg):
	print "send "+pirate_pickups[randint(0,len(pirate_pickups)-1)]+"\n"
elif msg and re.match(r'--pickupguy', msg):
	print "send "+chick_pickups[randint(0,len(chick_pickups)-1)]+"\n"
elif msg and re.match(r'--saying', msg):
	print "send "+pirate_sayings[randint(0,len(pirate_sayings)-1)]+"\n"
elif msg and re.match(r'(\S+)\s*$', msg) and chan:
	c = re.match(r'(\S+)\s*$', msg)
	print "send "+translate(gettext(chan,c.group(1)))+"\n"
elif msg and re.match(r'(\S+.*)', msg):
	print "send "+translate(msg)+"\n"
elif chan:
	print "send "+translate(gettext(chan))+"\n"

